package com.example.home_model;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.baseadapter_sdk.BaseQuickAdapter;
import com.example.baseadapter_sdk.BaseQuickMultipleAdapter;
import com.example.common.filed.DataActionEntity;
import com.example.common.mvvm.view.BaseFragment;
import com.example.home_model.databinding.FragmentVideoBinding;
import com.example.home_model.entity.DataDTO;
import com.example.home_model.entity.DataDTOX;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class VideoFragment extends BaseFragment<FragmentVideoBinding,VideoViewModel>
        implements BaseQuickMultipleAdapter.OnItemChildCLickListener, BaseQuickMultipleAdapter.OnItemCLickListener {


    private RecyclerView videoRv;
    private List<DataDTO> dataDTOList;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBind() {
        return FragmentVideoBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        videoRv = v.videoRv;
    }

    @Override
    public void initData() {

    }

    @Override
    public void receiveData(Map<String, Object> data) {
        DataDTOX dataDTOX = (DataDTOX) data.get("VIDEODATA");

        dataDTOList = dataDTOX.data;
        HomeMultipleAdapter adapter = (HomeMultipleAdapter) videoRv.getAdapter();
        if (adapter==null){
            adapter = new HomeMultipleAdapter();
            adapter.setOnItemChildCLickListener(this);
            adapter.setOnItemCLickListener(this);
            videoRv.setLayoutManager(new LinearLayoutManager(videoRv.getContext(), RecyclerView.VERTICAL,false));
            videoRv.setAdapter(adapter);

            adapter.setNewInstance(dataDTOList);

        }else {
            adapter.setNewInstance(dataDTOList);
        }

    }



    @Override
    public void onItemChildClick(RecyclerView.Adapter adapter, int id, int position) {
        HomeMultipleAdapter multipleAdapter = (HomeMultipleAdapter) adapter;
        List<DataDTO> data = ((HomeMultipleAdapter) adapter).getData();
        int itemType = data.get(position).itemType;
        long itemId = data.get(position).itemId;
        if (id == R.id.button_diss){
            ToastUtils.showLong("不喜歡");
        }else if (id == R.id.button_liek){
            Map<String, Object> like = new HashMap<String, Object>();
            like.put("itemId", itemId);
            like.put("itemType", itemType);
            like.put("userId", SPUtils.getInstance().getLong("userId"));
            MobclickAgent.onEventObject(getActivity().getApplicationContext(), "give_like", like);
        }else if (id == R.id.button_comment){
            ToastUtils.showLong("评论");
        }else if (id == R.id.button_share){
            new ShareAction(getActivity()).withText("hello").setPlatform(SHARE_MEDIA.SINA).share();
        }
    }

    @Override
    public void onItemClick(RecyclerView.Adapter adapter, int position) {
        HomeMultipleAdapter multipleAdapter = (HomeMultipleAdapter) adapter;
        List<DataDTO> data = multipleAdapter.getData();
        DataDTO dataDTO = data.get(position);
        ARouter.getInstance().build("/details/DetailsActivity").withSerializable("dataDTO",dataDTO).navigation();
    }


}